package com.blt.car.login;

import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.blt.car.MainActivity;
import com.blt.car.R;
import com.blt.car.base.BaseActivity;
import com.blt.car.base.ViewHolder;
import com.blt.car.utils.Uiutils;

import java.io.File;

import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.login.LoginApi;
import blt.app_core.log.Logger;
import blt.app_core.utils.CommonPref;
import blt.app_core.utils.PreferencesConstant;

/**
 * Created by xgp on 2017/6/15.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private EditText etPhone;
    private EditText etPwd;
    private CheckBox cbRememberPwd;

    private boolean isClickRemember;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView(ViewHolder holder) {
        holder.get(R.id.login_btn_login);
        holder.get(R.id.login_back);
        holder.get(R.id.login_forget_pwd);
        cbRememberPwd = holder.get(R.id.login_cb_remember_pwd);
        etPhone = holder.get(R.id.login_et_user);
        etPwd = holder.get(R.id.login_et_pwd);
        holder.setOnClickListener(this, R.id.login_btn_login, R.id.login_back, R.id.login_forget_pwd);

        cbRememberPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isClickRemember = isChecked;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_btn_login:
//                openActivity(MainActivity.class);
                String user = etPhone.getText().toString();
                String password = etPwd.getText().toString();
                if (TextUtils.isEmpty(user) || TextUtils.isEmpty(password)) {
                    toastShort("用户名和密码不能为空");
                    return;
                }
                Uiutils.progressShow(this);
                CoreManager.getCore(LoginApi.class).login(user, password);
                break;
            case R.id.login_forget_pwd:
                openActivity(RegisterActivity.class, RegisterActivity.ACTIVITY_NAME, 1);
                break;
            case R.id.login_back:
                finish();
                break;
        }

    }

    @CoreEvent(coreClientClass = LoginApi.class)
    public void onLogin() {
        String user = etPhone.getText().toString();
        String password = etPwd.getText().toString();
        if (isClickRemember) {
            CommonPref.getInstance().saveString(PreferencesConstant.PHONE, user);
            CommonPref.getInstance().saveString(PreferencesConstant.PASSWORD, password);
            CommonPref.getInstance().saveBoolean(PreferencesConstant.REMEMBER_PASSWORD, isClickRemember);
        } else {
            CommonPref.getInstance().saveBoolean(PreferencesConstant.REMEMBER_PASSWORD, isClickRemember);
        }
        Uiutils.progressCancel();
        openActivity(MainActivity.class);
        //登陆成功之后之前的activity全部去掉
        finish();
    }

}
